<template>
  <div class="box">
    <div class="title">
      <van-icon class="icon" name="arrow-left" @click="returnFn" />
      {{ bool ? "修改收货地址" : "新增收货地址" }}
    </div>
    <div class="content">
      <van-form @submit="onSubmit">
        <van-field
          v-model="info.form.name"
          name="name"
          label="姓名"
          placeholder="请输入收货人姓名"
          :rules="[{ required: true, message: '请填写收货人姓名' }]"
        />
        <van-field
          v-model="info.form.phone"
          name="phone"
          label="电话"
          placeholder="请输入收货人电话"
          :rules="[{ required: true, message: '请填写收货人电话' }]"
        />

        <van-field
          readonly
          clickable
          name="area"
          v-model="info.value"
          label="地区选择"
          placeholder="点击选择省市区"
          @click="showArea = true"
          :rules="[{ required: true, message: '请选择省市区' }]"
        />
        <van-popup v-model:show="showArea" position="bottom">
          <van-area
            :area-list="areaList"
            @confirm="onConfirm"
            @cancel="showArea = false"
          />
        </van-popup>

        <van-field
          v-model="info.form.address"
          name="name"
          label="详细地址"
          placeholder="请输入收货详细地址"
          :rules="[{ required: true, message: '请填写详细地址' }]"
        />

        <van-field name="switch" label="设为默认地址">
          <template #input>
            <van-switch
              v-model="info.form.defaultStatus"
              size="25"
              style="position: absolute; right: 10px; top: 3px"
              active-color="#F04949"
            />
          </template>
        </van-field>

        <div style="margin: 16px">
          <van-button
            class="button"
            round
            block
            type="info"
            native-type="submit"
            >{{ bool ? "修改" : "新增" }}</van-button
          >
        </div>
      </van-form>
    </div>
  </div>
</template>

<script>
import {
  defineComponent,
  getCurrentInstance,
  reactive,
  ref,
  onMounted,
} from "vue";
import { useRouter, useRoute } from "vue-router";
import { areaList } from "@vant/area-data";
import { addAddress, setAddress } from "@/api/http.js";

export default defineComponent({
  setup() {
    const { proxy } = getCurrentInstance();
    const router = useRouter();
    const route = useRoute();

    const showArea = ref(false);
    const onConfirm = (values) => {
      info.value = values
        .filter((item) => !!item)
        .map((item) => item.name)
        .join(" ");
      info.form.province = values[0].name;
      info.form.city = values[1].name;
      info.form.district = values[2].name;
      showArea.value = false;
      console.log("values---", values);
      console.log("value---", info.value);
    };

    const info = reactive({
      form: {
        name: "",
        phone: "",
        province: "", // 省
        city: "", // 市
        district: "", // 区
        address: "",
        defaultStatus: false,
      },
      value: "",
    });

    const bool = ref(false);

    const getInfo = () => {
      route.params.id ? (bool.value = true) : (bool.value = false);
      console.log(route.params);

      if (bool.value) {
        route.params.defaultStatus == "1"
          ? (route.params.defaultStatus = true)
          : (route.params.defaultStatus = false);

        info.value = `${route.params.province} ${route.params.city} ${route.params.district}`;

        info.form = route.params;
      }
      console.log("item---", info.form);
    };

    const onSubmit = async () => {
      info.form.defaultStatus
        ? (info.form.defaultStatus = 1)
        : (info.form.defaultStatus = 0);
      console.log("提交表单---", info.form);
      let data;
      if (bool.value) {
        data = await setAddress(info.form); // 修改
      } else {
        data = await addAddress(info.form); // 新增
      }
      if (data.code == 200) {
        proxy.$toast.success(data.msg);
        setTimeout(() => {
          returnFn();
        }, 2000);
      } else {
        proxy.$toast.fail(data.msg);
      }
      console.log(data);
    };

    const returnFn = () => {
      router.go(-1);
    };

    const selectFn = (item) => {
      console.log(item);
      returnFn();
    };

    const modifyFn = () => {
      returnFn();
    };

    onMounted(() => {
      getInfo();
    });

    return {
      returnFn,
      proxy,
      selectFn,
      modifyFn,
      route,
      info,
      showArea,
      onConfirm,
      onSubmit,
      areaList,
      bool,
    };
  },
});
</script>

<style lang="less" scoped>
.box {
  min-height: 100vh;
  background-color: var(--backColorTwo);
  color: #666666;
  .title {
    width: 100%;
    height: 60px;
    font-size: 1.125rem;
    font-family: PingFang SC;
    font-weight: bold;
    color: #fff;
    text-align: center;
    line-height: 60px;
    position: relative;
    padding: 0 4%;
    background-color: #212121;

    .icon {
      position: absolute;
      top: 50%;
      left: 10px;
      font-size: 1.5rem;
      transform: translateY(-50%);
    }
  }
  .content {
    padding: 20px 4%;
    color: var(--textColor);

    /deep/ .van-cell {
      background-color: transparent;
      font-size: 1rem;
      line-height: 32px;
    }
    /deep/ .van-field__label {
      color: var(--textColor);
    }
    /deep/ .van-field__control {
      color: var(--textColor);
    }

    /deep/ .van-field__button {
      margin-top: -8px;
    }
  }
  .button {
    height: 45px;
    background: #f04949;
    border-radius: 50px;
    font-size: 1.1rem;
    font-family: PingFang SC;
    font-weight: 400;
    color: #fff;
    text-align: center;
    line-height: 45px;
    border: none;
  }
}
</style>
